#include "contactdelegate.h"
#include <QPainter>
#include <QIcon>
#include <QFileInfo>
#include <QStyleOptionViewItem>
#include <QRect>
#include <QPixmap>
#include <QSqlRelationalDelegate>
#include <QLineEdit>
#include "contactform.h"

ContactDelegate::ContactDelegate(QObject *parent)
     : QSqlRelationalDelegate(parent)
{
}

void ContactDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                           const QModelIndex &index) const
 {
    int padding = 8;
    //get Data
    painter->save();
    painter->setFont(option.font);
    //Background
     if (option.state & QStyle::State_Selected) {
        painter->setBrush(option.palette.highlightedText());
        QPainterPath path;
        path.addRoundedRect(option.rect, 10, 10);
        painter->fillPath(path, option.palette.highlight());

     } else {
        QPainterPath path;
        path.addRoundedRect(option.rect, 10, 10);
        painter->fillPath(path, option.palette.base());
     }
     //icon
     QRect iconRect = QRect(option.rect.left()+padding,option.rect.top()+padding,64,64);
     QString iconPath = "images/faces/" + index.data(Qt::UserRole+3).toString() + ".png";
     QFileInfo iconInfo(iconPath);
    if(!iconInfo.exists())
         iconPath = "images/faces/empty.png";
     QPixmap face(iconPath);
     painter->drawPixmap(iconRect,face);

    //Draw some text
     int x = option.rect.left()+96+padding*2;
     int y = option.rect.top()+padding+18;
     if (option.state & QStyle::State_Selected) {
         QFont smaller = option.font;
         smaller.setPointSize(10);
         smaller.setWeight(2);
         painter->setFont(smaller);
         painter->drawText(x,y,index.data(Qt::DisplayRole).toString());
         y+= padding + 18;
         painter->drawText(x,y,index.data(Qt::UserRole+1).toString());
         y+= padding + 8;
         painter->drawText(x,y,index.data(Qt::UserRole+2).toString());

     } else {
         y= option.rect.top()+option.rect.height()/2;
         painter->drawText(x,y,index.data(Qt::DisplayRole).toString());
     }

     x = 0;
     y = 0;
     painter->restore();

 }

/*
QWidget * ContactDelegate::createEditor(QWidget *parent,
                                         const QStyleOptionViewItem & option,
                                         const QModelIndex & index) const
{
    ContactForm *editor = new ContactForm(parent);

    return editor;
}

void ContactDelegate::setEditorData(QWidget *editor,
                                     const QModelIndex &index) const
 {
    ContactForm *form = static_cast<ContactForm*>(editor);
    form->nameLine->insert(index.data(Qt::DisplayRole).toString());
    form->numberLine->insert(index.data(Qt::UserRole+2).toString());
    form->emailLine->insert(index.data(Qt::UserRole+1).toString());
 }


void ContactDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                    const QModelIndex &index) const
 {
     ContactForm *form = static_cast<ContactForm*>(editor);
     model->setData(index, form->nameLine->text(), Qt::EditRole);
     model->setData(index, form->numberLine->text(), Qt::UserRole+2);
     model->setData(index, form->emailLine->text(), Qt::UserRole+1);
 }

void ContactDelegate::updateEditorGeometry(QWidget *editor,
     const QStyleOptionViewItem &option, const QModelIndex & index) const
 {
     editor->setGeometry(option.rect);
 }
*/
QSize ContactDelegate::sizeHint(const QStyleOptionViewItem & option,
                               const QModelIndex & /* index */) const
 {
     return QSize(64, 80);
 }
